package com.demo.netty.mulport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

public class MulPortServer {
	
    private static final Logger logger = Logger.getLogger(MulPortServer.class);


	public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(3);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_BACKLOG, 1024);
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new ChannelInitializer<SocketChannel>(){
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					
					int port = ch.localAddress().getPort();
					logger.info("the socketChannel port is : " + port);
					if (port == Protocol.HTTP.getProtocolPort()){
				        ChannelPipeline p = ch.pipeline();
				        p.addLast(new HttpServerCodec());
				        p.addLast(new HttpObjectAggregator(65536));
				        p.addLast(new HttpHelloWorldServerHandler());
					}else if (port == Protocol.WEBSOCKET.getProtocolPort()){
				        ChannelPipeline p = ch.pipeline();
				        p.addLast(new IdleStateHandler(60, 30, 0));
				        p.addLast(new HttpServerCodec());
				        p.addLast(new HttpObjectAggregator(65536));
				        p.addLast(new WebSocketServerCompressionHandler());
				        p.addLast(new WebSocketServerProtocolHandler("/websocket", null, true));
				        p.addLast(new WebsocketHeartbeatHandler());
				        p.addLast(new WebSocketFrameHandler());
					}else if (port == Protocol.TCP.getProtocolPort()){
						
					}
					System.out.println(ch.localAddress());
				}
             });
            
            List<Integer> ports = Arrays.asList(Protocol.HTTP.getProtocolPort()
            		, Protocol.WEBSOCKET.getProtocolPort()
            		, Protocol.TCP.getProtocolPort());
            Collection<Channel> channels = new ArrayList<Channel>(ports.size());
            for (int port : ports) {
                Channel serverChannel = b.bind(port).sync().channel();
                channels.add(serverChannel);
            }
            for (Channel ch : channels) {
                ch.closeFuture().sync();
            }
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

		
		
		

	}

}
